package com.supply.service.impl;

import com.supply.mapper.RoleMapper;
import com.supply.mapper.RolePermissionMapper;
import com.supply.mapper.UserMapper;
import com.supply.pojo.*;
import com.supply.req.RoleGroup;
import com.supply.req.RoleReq;
import com.supply.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Role> queryByCondition(RoleReq roleReq) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();

        if (roleReq != null) {
            if (!ObjectUtils.isEmpty(roleReq.getName())) {
                criteria.andNameLike("%" + roleReq.getName() + "%");
            }
            if (!ObjectUtils.isEmpty(roleReq.getRemark())) {
                criteria.andRemarkLike("%" + roleReq.getRemark() + "%");
            }
            if (!ObjectUtils.isEmpty(roleReq.getDeptId())){
                criteria.andDeptIdEqualTo(roleReq.getDeptId());
            }
        }
        return roleMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(Role role) {
        List<Role> roles = roleMapper.selectByExample(null);
        if (role.getId() == null) {
            for (Role r : roles) {
                if (r.getName().equals(role.getName())) {
                    return 0;
                }
            }
            return roleMapper.insert(role);
        } else {
            for (Role r : roles) {
                if (r.getName().equals(role.getName()) && (r.getId() != role.getId())) {
                    return 0;
                }
            }
            return roleMapper.updateByPrimaryKey(role);
        }
    }

    @Override
    public int deleteById(Long id) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andRoleIdEqualTo(id);
        List<User> users = userMapper.selectByExample(userExample);
        if (!users.isEmpty()){
            return 0;
        }
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean authorizationByRoleId(RoleGroup roleGroup) {
//        先根据roleId删除所有关联权限
        RolePermissionExample example = new RolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(roleGroup.getId());
        rolePermissionMapper.deleteByExample(example);

        //再重新插入
        roleGroup.getPermissions().forEach(item -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleGroup.getId());
            rolePermission.setPermission(item);
            rolePermissionMapper.insert(rolePermission);
        });

        return true;
    }
}
